package com.zhihuitong.meeting.meeting.list;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zhihuitong.meeting.R;
import com.zhihuitong.meeting.dao.LocalMeetingBean;
import com.zhihuitong.meeting.utils.TimeUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者： guoyuan
 * 时间 2020/2/24.
 * 文件描述：
 * 版本号：V 1.0
 **/
public class MeetingListAdapter extends BaseQuickAdapter<LocalMeetingBean, BaseViewHolder> {
    @BindView(R.id.tv_subject)
    TextView mTvSubject;
    @BindView(R.id.tv_startTime)
    TextView mTvStartTime;

    @BindView(R.id.tv_state)
    TextView mTvState;
    @BindView(R.id.tv_createTime)
    TextView mTvCreateTime;
    @BindView(R.id.tv_scheduserName)
    TextView mTvScheduserName;
    @BindView(R.id.btn_joinMeeting)
    Button mBtnJoinMeeting;
    @BindView(R.id.btn_shareMeeting)
    Button mBtnShareMeeting;
    @BindView(R.id.tv_endTime)
    TextView mTvEndTime;
    @BindView(R.id.tv_chairPwd)
    TextView mTvChairPwd;
    @BindView(R.id.tv_meetingId)
    TextView mTvMeetingId;

    public MeetingListAdapter(@Nullable List<LocalMeetingBean> data) {
        super(R.layout.item_meeting_list_view, data);
    }

    /**
     * Implement this method and use the helper to adapt the view to the given item.
     *
     * @param helper A fully initialized helper.
     * @param item   The item that needs to be displayed.
     */
    @Override
    protected void convert(BaseViewHolder helper, LocalMeetingBean item) {
        ButterKnife.bind(this, helper.itemView);
        if (TextUtils.isEmpty(item.getEndTime())) {
            item.setEndTime("1970-01-01 00:00");
        }
        if (TextUtils.isEmpty(item.getStartTime())) {
            item.setStartTime("1970-01-01 00:00");
        }
        mTvSubject.setText("会议主题: " + item.getSubject());
        mTvStartTime.setText("开始时间: " + item.getStartTime());
        mTvEndTime.setText("结束时间: " + item.getEndTime());
        mTvMeetingId.setText("会议ID: " + item.getConferenceID());

        String endTime = item.getEndTime();
        Date date = TimeUtils.string2Date(endTime, new SimpleDateFormat("yyyy-MM-dd HH:mm"));
        if (date == null) {
            mTvState.setText("会议状态: 结束时间异常");
            mBtnJoinMeeting.setVisibility(View.GONE);
            mBtnShareMeeting.setVisibility(View.GONE);
        } else if (System.currentTimeMillis() >= date.getTime()) {
            //系统时间大于结束时间,表示会议结束
            mTvState.setText("会议状态: 已结束");
            mBtnJoinMeeting.setVisibility(View.GONE);
            mBtnShareMeeting.setVisibility(View.GONE);
        } else {
            helper.addOnClickListener(R.id.btn_joinMeeting);
            helper.addOnClickListener(R.id.btn_shareMeeting);
            mBtnJoinMeeting.setVisibility(View.VISIBLE);
            mBtnShareMeeting.setVisibility(View.VISIBLE);
            //系统时间小于结束时间,但是大于开始时间,表示会议进行中
            if (System.currentTimeMillis() >= TimeUtils.string2Date(item.getStartTime(), new SimpleDateFormat("yyyy-MM-dd HH:mm")).getTime()) {
                mTvState.setText("会议状态: 进行中");
            } else {
                mTvState.setText("会议状态: 预订中");
            }
        }
        mTvChairPwd.setText("主席密码: " + item.getChairPwd() + "     来宾密码: " + item.getGeneralPwd());
        mTvCreateTime.setText("创建时间: " + TimeUtils.date2String(new Date(item.getCreateTime())));
        mTvScheduserName.setText("创建人: " + item.getScheduserName());

    }
}
